package cn.maihe.elg.operation.supports.bank;

import cn.maihe.elg.operation.config.CmbBankConfig;
import cn.maihe.elg.operation.config.XibBankConfig;
import cn.maihe.elg.operation.model.dto.BaseReq;
import cn.maihe.elg.operation.model.dto.BaseResp;
import cn.maihe.elg.operation.supports.bank.cmb.dto.*;
import cn.maihe.elg.operation.supports.bank.cmb.util.DcHelper;
import cn.maihe.elg.operation.supports.bank.dto.XibAccountDetailReqDTO;
import cn.maihe.elg.operation.supports.bank.dto.XibAccountDetailRespDTO;
import cn.maihe.elg.operation.supports.bank.enums.CmbMethod;
import cn.maihe.elg.operation.supports.bank.enums.SpdbMethod;
import cn.maihe.elg.operation.supports.bank.enums.XibMethod;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.ElgLogger;
import cn.maihe.elg.operation.utils.IdGenerate;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.open.sdk.OpenSDK;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.Test;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.security.Security;

/**
 * @Description 账务查询 7.账户交易信息查询trsQryByBreakPoint 支持查近13个月内对公客户金融交易数据。
 * @Author Alan
 * @Date 2024/11/12
 */
@Service
@Slf4j
public abstract class BaseCmbBankTransService<REQ extends BaseReq, RESP extends BaseResp> {

    protected Class<REQ> reqClass;
    protected Class<RESP> respClass;
//    protected CommonResp<RESP> commonRespClass;

    @Resource
    protected CmbBankConfig cmbBankConfig;
    @Resource
    protected ElgResourceService elgResourceService;

    protected abstract CmbMethod cmbMethod();

    public BaseCmbBankTransService() {
        Type[] types = ((ParameterizedType) (getClass().getGenericSuperclass())).getActualTypeArguments();
        this.reqClass = (Class<REQ>) types[0];
        this.respClass = (Class<RESP>) types[1];
//        this.commonRespClass = new CommonRes;
        // 装载BC库,必须在应用的启动类中调用此函数
        Security.addProvider(new BouncyCastleProvider());
        System.setProperty("sun.net.http.retryPost", "false");
    }

//    public CommonReq toReq(Object obj) {
//        return JSON.parseObject(JSON.toJSONBytes(obj), this.reqClass);
//    }

    public String sendCmbData(CommonReq req) {
        String traceId = IdGenerate.genTranceId();
        String response;
//        CommonResp commonResp;
        try {
            String data = JSON.toJSONString(req);
            ElgLogger.info(log, l -> l.info("{} << {} 招商银行请求报文原文:{}", traceId, cmbMethod(), data));
            DcHelper dchelper = new DcHelper(cmbBankConfig.getUrl(), cmbBankConfig.getUid(),
                    cmbBankConfig.getPrivateKey(), cmbBankConfig.getPublicKey(), cmbBankConfig.getSymKey());
            response = dchelper.sendRequest(data, cmbMethod().getMethodId());
            Assert.notNull(response, () -> "招商银行响应报文为空");
//            System.out.println("response===" + response);
            ElgLogger.info(log, l -> l.info("{} << {} 招商银行响应报文原文:{}", traceId, cmbMethod(), response));
//            commonResp = JSON.parseObject(response, new TypeReference<CommonResp>() {
//            });
//            commonResp = JSON.parseObject(response, this.respClass);
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("{} << {} 请求响应异常:{}", traceId, cmbMethod(), e.getMessage(), e));
            throw new RuntimeException("处理异常:" + e.getMessage());
        }
        return response;
    }

    @Test
    public void openSDKTest() throws Exception {
        String privateKey = "ANMNzr5tmnUsufjET6yotnNKU/nAvzidG7OKoE6GaTcj";
        String path = this.getClass().getClassLoader().getResource("config/config-dev.properties").getPath();
        OpenSDK.init(path, privateKey);
        String pdId = "ZHFW0002";   //产品ID ZF]S0004
        String apiId = "OPXM0001A201000700000100";  //接口ID
        String data = "{\"pageNo\":1,\"transTimeEnd\":\"20240827212700\",\"pageSize\":50,\"custAcct\":\"8005100000017168\",\"transTimeStart\":\"20240827000000\"}";
        OpenSDK.sendData(pdId, apiId, data);
    }
}
